﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace EdgeStatz.ESSystems.Objects
{
    public class SendEmail : Form
    {
        #region Designer
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.tb_To = new System.Windows.Forms.TextBox();
            this.tb_Cc = new System.Windows.Forms.TextBox();
            this.tb_Bcc = new System.Windows.Forms.TextBox();
            this.label4 = new System.Windows.Forms.Label();
            this.tb_Subject = new System.Windows.Forms.TextBox();
            this.tb_Body = new System.Windows.Forms.TextBox();
            this.b_Cancel = new System.Windows.Forms.Button();
            this.b_Send = new System.Windows.Forms.Button();
            this.label5 = new System.Windows.Forms.Label();
            this.tb_Attachments = new System.Windows.Forms.TextBox();
            this.b_AddAttachment = new System.Windows.Forms.Button();
            this.b_To = new System.Windows.Forms.Button();
            this.b_Cc = new System.Windows.Forms.Button();
            this.b_Bcc = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // tb_To
            // 
            this.tb_To.Location = new System.Drawing.Point(87, 6);
            this.tb_To.Name = "tb_To";
            this.tb_To.Size = new System.Drawing.Size(599, 20);
            this.tb_To.TabIndex = 0;
            // 
            // tb_Cc
            // 
            this.tb_Cc.Location = new System.Drawing.Point(87, 35);
            this.tb_Cc.Name = "tb_Cc";
            this.tb_Cc.Size = new System.Drawing.Size(599, 20);
            this.tb_Cc.TabIndex = 2;
            // 
            // tb_Bcc
            // 
            this.tb_Bcc.Location = new System.Drawing.Point(87, 64);
            this.tb_Bcc.Name = "tb_Bcc";
            this.tb_Bcc.Size = new System.Drawing.Size(599, 20);
            this.tb_Bcc.TabIndex = 5;
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(12, 93);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(46, 13);
            this.label4.TabIndex = 6;
            this.label4.Text = "Subject:";
            // 
            // tb_Subject
            // 
            this.tb_Subject.Location = new System.Drawing.Point(87, 90);
            this.tb_Subject.Name = "tb_Subject";
            this.tb_Subject.Size = new System.Drawing.Size(599, 20);
            this.tb_Subject.TabIndex = 7;
            // 
            // tb_Body
            // 
            this.tb_Body.Location = new System.Drawing.Point(15, 155);
            this.tb_Body.Multiline = true;
            this.tb_Body.Name = "tb_Body";
            this.tb_Body.Size = new System.Drawing.Size(671, 336);
            this.tb_Body.TabIndex = 8;
            // 
            // b_Cancel
            // 
            this.b_Cancel.Location = new System.Drawing.Point(611, 497);
            this.b_Cancel.Name = "b_Cancel";
            this.b_Cancel.Size = new System.Drawing.Size(75, 23);
            this.b_Cancel.TabIndex = 9;
            this.b_Cancel.Text = "Cancel";
            this.b_Cancel.UseVisualStyleBackColor = true;
            this.b_Cancel.Click += new System.EventHandler(this.b_Cancel_Click);
            // 
            // b_Send
            // 
            this.b_Send.Location = new System.Drawing.Point(530, 497);
            this.b_Send.Name = "b_Send";
            this.b_Send.Size = new System.Drawing.Size(75, 23);
            this.b_Send.TabIndex = 10;
            this.b_Send.Text = "Send";
            this.b_Send.UseVisualStyleBackColor = true;
            this.b_Send.Click += new System.EventHandler(this.b_Send_Click);
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(12, 119);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(69, 13);
            this.label5.TabIndex = 11;
            this.label5.Text = "Attachments:";
            // 
            // tb_Attachments
            // 
            this.tb_Attachments.Location = new System.Drawing.Point(87, 116);
            this.tb_Attachments.Name = "tb_Attachments";
            this.tb_Attachments.Size = new System.Drawing.Size(518, 20);
            this.tb_Attachments.TabIndex = 12;
            // 
            // b_AddAttachment
            // 
            this.b_AddAttachment.Location = new System.Drawing.Point(611, 114);
            this.b_AddAttachment.Name = "b_AddAttachment";
            this.b_AddAttachment.Size = new System.Drawing.Size(75, 23);
            this.b_AddAttachment.TabIndex = 13;
            this.b_AddAttachment.Text = "Add...";
            this.b_AddAttachment.UseVisualStyleBackColor = true;
            this.b_AddAttachment.Click += new System.EventHandler(this.b_AddAttachment_Click);
            // 
            // b_To
            // 
            this.b_To.Location = new System.Drawing.Point(15, 4);
            this.b_To.Name = "b_To";
            this.b_To.Size = new System.Drawing.Size(66, 23);
            this.b_To.TabIndex = 14;
            this.b_To.Text = "To...";
            this.b_To.UseVisualStyleBackColor = true;
            this.b_To.Click += new System.EventHandler(this.b_AddressManager_Click);
            // 
            // b_Cc
            // 
            this.b_Cc.Location = new System.Drawing.Point(15, 33);
            this.b_Cc.Name = "b_Cc";
            this.b_Cc.Size = new System.Drawing.Size(66, 23);
            this.b_Cc.TabIndex = 15;
            this.b_Cc.Text = "Cc...";
            this.b_Cc.UseVisualStyleBackColor = true;
            this.b_Cc.Click += new System.EventHandler(this.b_AddressManager_Click);
            // 
            // b_Bcc
            // 
            this.b_Bcc.Location = new System.Drawing.Point(15, 62);
            this.b_Bcc.Name = "b_Bcc";
            this.b_Bcc.Size = new System.Drawing.Size(66, 23);
            this.b_Bcc.TabIndex = 16;
            this.b_Bcc.Text = "Bcc...";
            this.b_Bcc.UseVisualStyleBackColor = true;
            this.b_Bcc.Click += new System.EventHandler(this.b_AddressManager_Click);
            // 
            // SendEmail
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(698, 532);
            this.ControlBox = false;
            this.Controls.Add(this.b_Bcc);
            this.Controls.Add(this.b_Cc);
            this.Controls.Add(this.b_To);
            this.Controls.Add(this.b_AddAttachment);
            this.Controls.Add(this.tb_Attachments);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.b_Send);
            this.Controls.Add(this.b_Cancel);
            this.Controls.Add(this.tb_Body);
            this.Controls.Add(this.tb_Subject);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.tb_Bcc);
            this.Controls.Add(this.tb_Cc);
            this.Controls.Add(this.tb_To);
            this.Name = "SendEmail";
            this.Text = "SendEmail";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private System.Windows.Forms.TextBox tb_To;
        private System.Windows.Forms.TextBox tb_Cc;
        private System.Windows.Forms.TextBox tb_Bcc;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.TextBox tb_Subject;
        private System.Windows.Forms.TextBox tb_Body;
        private System.Windows.Forms.Button b_Cancel;
        private Label label5;
        private TextBox tb_Attachments;
        private Button b_AddAttachment;
        private Button b_To;
        private Button b_Cc;
        private Button b_Bcc;
        private System.Windows.Forms.Button b_Send;

        #endregion

        List<string> groupList = new List<string> { };
        List<DisplayValueObject[]> itemList = new List<DisplayValueObject[]> { };

        public SendEmail()
        {
            InitializeComponent();
        }
        
        public SendEmail(List<string> groupNames, List<string[]> itemNames, List<string[]> itemAddresses)
            : this()
        {
            if (groupNames.Count != itemNames.Count && groupNames.Count != itemAddresses.Count)
                throw new Exception("The number of email groups do not match the number of items groups.");
            for(int i=0;i<groupNames.Count;i++)
            {
                this.groupList.Add(groupNames[i]);
                string[] names = itemNames[i];
                string[] add =itemAddresses[i];
                if(names.Length != add.Length)
                    throw new Exception("The number of email address do not match the number of items in the group.");
                DisplayValueObject[] array = new DisplayValueObject[names.Length];
                for (int j = 0; j < names.Length; j++)
                    array[j] = new DisplayValueObject(names[j], add[j]);
                this.itemList.Add(array);
            }
        }
        public SendEmail(string[] to)
            : this()
        {
            tb_To.Text = "";
            foreach(string str in to)
                tb_To.Text += str.Trim().Equals("") ? "" : str + "; ";
        }
        public SendEmail(string to) : this(new string[] { to }) { }

        private void b_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void b_Send_Click(object sender, EventArgs e)
        {
            if (tb_To.Text.Trim().Equals(""))
            {
                MessageBox.Show("Please specify at least one recipient.", "Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            } 
            if (tb_Subject.Text.Trim().Equals(""))
            {
                DialogResult dr = MessageBox.Show("Send this message without a subject?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dr == DialogResult.No)
                    return;
            }
            if (tb_Body.Text.Trim().Equals(""))
            {
                DialogResult dr = MessageBox.Show("Send this message without text in the body?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dr == DialogResult.No)
                    return; 
            }
            try
            {
                Email.SendMessage(tb_Subject.Text, tb_Body.Text, tb_To.Text, tb_Cc.Text, tb_Bcc.Text, tb_Attachments.Text.Split(new char[]{';'}));
                MessageBox.Show("Email(s) have been sent successfully.");
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ": \n" + ex.InnerException);
            }
        }

        private void b_AddAttachment_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Add Attachments";
            ofd.Multiselect = true;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                foreach (string fn in ofd.FileNames)
                    tb_Attachments.Text += fn + ";";
            }
        }

        private void b_AddressManager_Click(object sender, EventArgs e)
        {
            EmailAddresses manager = new EmailAddresses(tb_To.Text, tb_Cc.Text, tb_Bcc.Text);
            for (int i = 0; i < groupList.Count; i++)
                manager.AddGroup(groupList[i], itemList[i]);
            if (manager.ShowDialog() == DialogResult.OK)
            {
                tb_To.Text = manager.To;
                tb_Cc.Text = manager.Cc;
                tb_Bcc.Text = manager.Bcc;
            }
        }
    }

    #region Address Manager form
    class EmailAddresses : Form
    {
        List<string> groupsList = new List<string> { };
        List<DisplayValueObject[]> itemsList = new List<DisplayValueObject[]> { };

        public string To 
        { 
            get { return tb_To.Text; } 
            set 
            {
                tb_To.Text = value;
                if (!tb_To.Text.Trim().Equals(""))
                    if (!tb_To.Text.Trim().EndsWith(";"))
                        tb_To.Text += "; ";
            } 
        }
        public string Cc
        {
            get { return tb_Cc.Text; }
            set
            {
                tb_Cc.Text = value;
                if (!tb_Cc.Text.Trim().Equals(""))
                    if (!tb_Cc.Text.Trim().EndsWith(";"))
                        tb_Cc.Text += "; ";
            }
        }
        public string Bcc
        {
            get { return tb_Bcc.Text; }
            set
            {
                tb_Bcc.Text = value;
                if (!tb_Bcc.Text.Trim().Equals(""))
                    if (!tb_Bcc.Text.Trim().EndsWith(";"))
                        tb_Bcc.Text += "; ";
            }
        }

        string focus = "TO";

        public EmailAddresses(string to, string cc, string bcc)
        {
            InitializeComponent();
            this.To = to;
            this.Cc = cc;
            this.Bcc = bcc;
            RefreshGroups();
        }

        public void AddGroup(string groupname, DisplayValueObject[] items)
        {
            this.groupsList.Add(groupname);
            this.itemsList.Add(items);
            RefreshGroups();
        }

        public void RefreshGroups()
        {
            lb_Groups.Items.Clear();
            foreach (string name in groupsList)
                lb_Groups.Items.Add(name);

            lb_Items.Items.Clear();
            lb_Items.Enabled = false;
        }

        private void b_Ok_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void b_Cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void lb_Groups_SelectedIndexChanged(object sender, EventArgs e)
        {
            lb_Items.Items.Clear();
            lb_Items.Enabled = true;
            if (lb_Groups.SelectedIndex == -1)
            {
                lb_Items.Enabled = false;
                return;
            }
            foreach (DisplayValueObject dvo in itemsList[lb_Groups.SelectedIndex])
                lb_Items.Items.Add(dvo.Display);
        }

        private void lb_Items_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (sender == null && lb_Items.SelectedIndex == -1 && lb_Groups.SelectedIndex != -1)
            {
                for (int i = 0; i < itemsList[lb_Groups.SelectedIndex].Length; i++)
                {
                    lb_Items.SelectedIndex = i;
                    lb_Items_MouseDoubleClick(null, null);
                }
                lb_Items.SelectedIndex = -1;
            }
            else if (lb_Items.SelectedIndex != -1)
            {
                DisplayValueObject[] tmp = itemsList[lb_Groups.SelectedIndex];
                string address = tmp[lb_Items.SelectedIndex].Value;
                switch (focus.ToUpper())
                {
                    case "TO":
                        tb_To.Text += address + "; ";
                        break;
                    case "CC":
                        tb_Cc.Text += address + "; ";
                        break;
                    case "BCC":
                        tb_Bcc.Text += address + "; ";
                        break;
                    default:
                        goto case "TO";
                }
            }
        }

        private void b_To_Click(object sender, EventArgs e)
        {
            focus = "TO";
            lb_Items_MouseDoubleClick(null, null);
        }

        private void b_Cc_Click(object sender, EventArgs e)
        {
            focus = "CC";
            lb_Items_MouseDoubleClick(null, null);
        }

        private void b_Bcc_Click(object sender, EventArgs e)
        {
            focus = "BCC";
            lb_Items_MouseDoubleClick(null, null);
        }

        #region designer
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.tb_To = new System.Windows.Forms.TextBox();
            this.tb_Cc = new System.Windows.Forms.TextBox();
            this.tb_Bcc = new System.Windows.Forms.TextBox();
            this.b_Cancel = new System.Windows.Forms.Button();
            this.b_Ok = new System.Windows.Forms.Button();
            this.lb_Items = new System.Windows.Forms.ListBox();
            this.lb_Groups = new System.Windows.Forms.ListBox();
            this.b_To = new System.Windows.Forms.Button();
            this.b_Cc = new System.Windows.Forms.Button();
            this.b_Bcc = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // tb_To
            // 
            this.tb_To.Location = new System.Drawing.Point(93, 258);
            this.tb_To.Name = "tb_To";
            this.tb_To.Size = new System.Drawing.Size(460, 20);
            this.tb_To.TabIndex = 3;
            // 
            // tb_Cc
            // 
            this.tb_Cc.Location = new System.Drawing.Point(93, 287);
            this.tb_Cc.Name = "tb_CC";
            this.tb_Cc.Size = new System.Drawing.Size(460, 20);
            this.tb_Cc.TabIndex = 4;
            // 
            // tb_Bcc
            // 
            this.tb_Bcc.Location = new System.Drawing.Point(93, 316);
            this.tb_Bcc.Name = "tb_BCC";
            this.tb_Bcc.Size = new System.Drawing.Size(460, 20);
            this.tb_Bcc.TabIndex = 5;
            // 
            // b_Cancel
            // 
            this.b_Cancel.Location = new System.Drawing.Point(478, 343);
            this.b_Cancel.Name = "b_Cancel";
            this.b_Cancel.Size = new System.Drawing.Size(75, 23);
            this.b_Cancel.TabIndex = 8;
            this.b_Cancel.Text = "Cancel";
            this.b_Cancel.UseVisualStyleBackColor = true;
            this.b_Cancel.Click += new System.EventHandler(this.b_Cancel_Click);
            // 
            // b_Ok
            // 
            this.b_Ok.Location = new System.Drawing.Point(397, 343);
            this.b_Ok.Name = "b_Ok";
            this.b_Ok.Size = new System.Drawing.Size(75, 23);
            this.b_Ok.TabIndex = 9;
            this.b_Ok.Text = "OK";
            this.b_Ok.UseVisualStyleBackColor = true;
            this.b_Ok.Click += new System.EventHandler(this.b_Ok_Click);
            // 
            // lb_Items
            // 
            this.lb_Items.FormattingEnabled = true;
            this.lb_Items.Location = new System.Drawing.Point(208, 12);
            this.lb_Items.Name = "lb_Items";
            this.lb_Items.Size = new System.Drawing.Size(345, 238);
            this.lb_Items.TabIndex = 10;
            this.lb_Items.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.lb_Items_MouseDoubleClick);
            // 
            // lb_Groups
            // 
            this.lb_Groups.FormattingEnabled = true;
            this.lb_Groups.Location = new System.Drawing.Point(12, 12);
            this.lb_Groups.Name = "lb_Groups";
            this.lb_Groups.Size = new System.Drawing.Size(190, 238);
            this.lb_Groups.TabIndex = 11;
            this.lb_Groups.SelectedIndexChanged += new System.EventHandler(this.lb_Groups_SelectedIndexChanged);
            // 
            // b_To
            // 
            this.b_To.Location = new System.Drawing.Point(12, 256);
            this.b_To.Name = "b_To";
            this.b_To.Size = new System.Drawing.Size(75, 23);
            this.b_To.TabIndex = 12;
            this.b_To.Text = "To - >";
            this.b_To.UseVisualStyleBackColor = true;
            this.b_To.Click += new System.EventHandler(this.b_To_Click);
            // 
            // b_Cc
            // 
            this.b_Cc.Location = new System.Drawing.Point(12, 285);
            this.b_Cc.Name = "b_Cc";
            this.b_Cc.Size = new System.Drawing.Size(75, 23);
            this.b_Cc.TabIndex = 13;
            this.b_Cc.Text = "Cc - >";
            this.b_Cc.UseVisualStyleBackColor = true;
            this.b_Cc.Click += new System.EventHandler(this.b_Cc_Click);
            // 
            // b_Bcc
            // 
            this.b_Bcc.Location = new System.Drawing.Point(12, 314);
            this.b_Bcc.Name = "b_Bcc";
            this.b_Bcc.Size = new System.Drawing.Size(75, 23);
            this.b_Bcc.TabIndex = 14;
            this.b_Bcc.Text = "Bcc - >";
            this.b_Bcc.UseVisualStyleBackColor = true;
            this.b_Bcc.Click += new System.EventHandler(this.b_Bcc_Click);
            // 
            // EmailAddresses
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(565, 377);
            this.ControlBox = false;
            this.Controls.Add(this.b_Bcc);
            this.Controls.Add(this.b_Cc);
            this.Controls.Add(this.b_To);
            this.Controls.Add(this.lb_Groups);
            this.Controls.Add(this.lb_Items);
            this.Controls.Add(this.b_Ok);
            this.Controls.Add(this.b_Cancel);
            this.Controls.Add(this.tb_Bcc);
            this.Controls.Add(this.tb_Cc);
            this.Controls.Add(this.tb_To);
            this.Name = "EmailAddresses";
            this.Text = "EmailAddresses";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox tb_To;
        private System.Windows.Forms.TextBox tb_Cc;
        private System.Windows.Forms.TextBox tb_Bcc;
        private System.Windows.Forms.Button b_Cancel;
        private System.Windows.Forms.Button b_Ok;
        private System.Windows.Forms.ListBox lb_Items;
        private System.Windows.Forms.ListBox lb_Groups;
        private System.Windows.Forms.Button b_To;
        private System.Windows.Forms.Button b_Cc;
        private System.Windows.Forms.Button b_Bcc;
        #endregion
    }
    #endregion
}
